<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Conversation extends MY_Controller {

    public function __construct() {
      parent::__construct();
      
      $this->load->model('users_mod');
      $this->load->model('messages_mod');
      $this->load->model('images_mod');
    }
	/**
	 *
	 */
	public function index()
	{       
        $this->conversation_list();
	}
    
    public function conversation_list()
    {
        $data['title'] = lang('conversation_title');
        $data['keywords'] = lang('conversation_keywords');
        $data['description'] = lang('conversation_description');
        try{
          $id = $this->session->userdata('user_id');
          $data['user'] = $this->users_mod->getUserData($id,false);
          $data['main_image'] = $this->images_mod->getMainImage($id);
          $data['conversations'] = $this->messages_mod->getMyConversationList();
        } 
        catch( Exception $e )
        {
          $data['user'] = NULL;
          $data['main_image'] = NULL;
          $data['conversations'] = NULL;
        }
        
        $data['menu_active'] = "conversation";        
        $data['contents']  = "conversation/conversation";   
		$this->load->view('template/main',$data);
    }
    
    
    public function messages($from_username)
    {
        $data['title'] = lang('messages_title');
        $data['keywords'] = lang('messages_keywords');
        $data['description'] = lang('messages_description');
        try{
          if( $from_username != NULL )
          {
            $user_id = $this->session->userdata('user_id');
            $user = $this->users_mod->getUserData($user_id,false);
            $data['user'] = $user;
            $data['main_image'] = $this->images_mod->getMainImage($user_id);
          
//            $user_from = $this->users_mod->getUserDataByUsername($from_username,false);
//            $data['user_from'] = $user_from;
//            $data['user'] = $user_from;
//            $data['main_from_image'] = $this->images_mod->getMainImage($user_from->id);
            
            $user_from = $this->users_mod->getUserDataByUsername($from_username,false);
        
            if( NULL == $user_from ) // Nea podataka za tog usera idi na HOME page
              redirect(base_url());
            
            $data['user_from'] = $user_from;
            //$data['images'] = $this->images_mod->getUserImages($user->id,false);
            $data['main_from_image'] = $this->images_mod->getMainImage($user_from->id);
            
            $data['is_friend'] = $this->friends_mod->isMyFriend($user_id,$user_from->id);
            $data['is_favorite'] = $this->favorites_mod->isMyFavorite($user_id,$user_from->id);
            
            $data['messages'] = $this->messages_mod->getMessages($user_from->id);
          }
          else
          {
            $data['user'] = NULL;
            $data['main_image'] = NULL;
            $data['user_from'] = NULL;          
            $data['main_from_image'] = NULL;
            $data['is_friend'] = NULL;
            $data['is_favorite'] = NULL;
            $data['messages'] = NULL;
          }
        } 
        catch( Exception $e )
        {
          $data['user'] = NULL;
          $data['main_image'] = NULL;
          $data['user_from'] = NULL;          
          $data['main_from_image'] = NULL;
          $data['messages'] = NULL;
        }
        
        $data['menu_active'] = "conversation";        
        $data['contents']  = "conversation/messages";   
		$this->load->view('template/main',$data);
    }
    
    
    /**
     *  AJAX send message
     */
    public function send_message()
    {
        try{
          
          $to = $this->input->post('to');
          $message = $this->input->post('message');
          $from = $this->session->userdata('user_id');
          $ret = $this->messages_mod->sendMessage($from,$to,$message);
          if( 1 == $ret )
          {
            $data['main_image'] = $this->images_mod->getMainImage($from);
            $data['main_from_image'] = $this->images_mod->getMainImage($to);
            $data['user'] = $this->users_mod->getUserData($from,false);
            $data['user_from'] = $this->users_mod->getUserData($to,false);
            $data['messages'] = $this->messages_mod->getMessages($to,false);
            $html = $this->load->view('conversation/part/messages_list',$data,true);
            
            $response = array(
                'ind' => 1,
                'html' => $html
            );
          }
          else if( -2 == $ret)
          {
            $response = array(
                'ind' => -2,
                'msg' => sprintf(lang('user_no_coins'),base_url()."payment.htm")
            );
          }
          else
          {
            $response = array(
                'ind' => 0,
                'msg' => lang('message_not_send')
            );
          }
        } 
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
    
    public function remove_conversation()
    {
        try{
          
          $from_id = $this->input->post('from_id');
          $to_id = $this->input->post('to_id');          
          $rez = $this->messages_mod->remove_conversation($from_id,$to_id);
          if( true == $rez )
          {
            $data['conversations'] = $this->messages_mod->getMyConversationList();
            $html = $this->load->view('conversation/part/conversation_list',$data,true);
            
            $response = array(
                'ind' => 1,
                'html' => $html
            );
          }
          else
          {
            $response = array(
                'ind' => 0,
                'msg' => lang('conversation_not_delete')
            );
          }
        } 
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
    public function getMessages()
    {
      try{
        $from_username = $this->input->post('user_from_name');
        
        $user_id = $this->session->userdata('user_id');
        $user = $this->users_mod->getUserData($user_id,false);
        $data['user'] = $user;
        $data['main_image'] = $this->images_mod->getMainImage($user_id);

        $user_from = $this->users_mod->getUserDataByUsername($from_username,false);

        $data['user_from'] = $user_from;
        //$data['images'] = $this->images_mod->getUserImages($user->id,false);
        $data['main_from_image'] = $this->images_mod->getMainImage($user_from->id);

        //$data['is_friend'] = $this->friends_mod->isMyFriend($user_id,$user_from->id);
        //$data['is_favorite'] = $this->favorites_mod->isMyFavorite($user_id,$user_from->id);

        $data['messages'] = $this->messages_mod->getMessages($user_from->id);
        //print_r($data);
        $html = $this->load->view('conversation/part/messages_list',$data,true);
        //echo $html;    
            $response = array(
                'ind' => 1,
                'html' => $html
            );
        
      }
      catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
      }
        
      echo json_encode($response); 
    }
    
} // end class
/* End of file conversation.php */
/* Location: ./application/controllers/conversation.php */